package org.shopen.puremvc.view
{
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.patterns.mediator.Mediator;
	import org.shopen.puremvc.ApplicationConstants;
	import org.shopen.puremvc.view.components.toolbox.ToolBox;
	import org.shopen.puremvc.view.components.toolbox.events.ToolBoxWindowEvent;
	import org.shopen.puremvc.view.components.toolbox.events.ToolDeselectedEvent;
	import org.shopen.puremvc.view.components.toolbox.events.ToolSelectedEvent;

	public class ToolboxMediator extends Mediator
	{
		public static const NAME : String = "ToolboxMediator";
		
		protected var toolbox : ToolBox = null;
		
		protected function get parent() : DisplayObject
		{
			var result : DisplayObject = viewComponent as DisplayObject;
			return result;
		}
		
		public function ToolboxMediator(viewComponent:Object=null)
		{
			super(ToolboxMediator.NAME viewComponent);
		}
		
		override public function getMediatorName():String
		{
			return ToolboxMediator.NAME;
		}
		
		override public function onRegister():void
		{
			showToolbox();
		}
		
		override public function onRemove():void
		{
			removeToolbox();
		}
		
		override public function listNotificationInterests():Array
		{
			return [ApplicationConstants.SHOW_TOOLBOX,
					ApplicationConstants.HIDE_TOOLBOX,
					ApplicationConstants.POSITION_TOOLBOX];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			var body : Object = notification.getBody();
			switch(notification.getName())
			{
				case ApplicationConstants.SHOW_TOOLBOX:
					var data : XML = body as XML;
					showToolbox(data);
					break;
				case ApplicationConstants.HIDE_TOOLBOX:
					hideToolbox();
					break;
				case ApplicationConstants.POSITION_TOOLBOX:
					var x : Number = body.hasOwnProperty("x") ? Number(body.x) : NaN;
					var y : Number = body.hasOwnProperty("y") ? Number(body.y) : NaN;
					positionToolbox(x, y);
			}
		}
		
		protected function showToolbox(data : XML = null) : void
		{
			if (toolbox == null)
			{
				toolbox = createToolbox(data);
			}
			else
			{
				if (!toolbox.isPopUp)
				{					
					PopUpManager.addPopUp(toolbox, parent, false);
				}
			}
			PopUpManager.bringToFront(toolbox);
			PopUpManager.centerPopUp(toolbox);
			
			sendNotification(ApplicationConstants.TOOLBOX_SHOWN);
		}
		
		
		protected function createToolbox(data : XML = null) : ToolBox
		{
			var result : ToolBox = new ToolBox();
			if (data != null)
			{
				result.init(data);
			}
			result.addEventListener(ToolBoxWindowEvent.CLOSE, toolboxCloseListener);
			result.addEventListener(ToolSelectedEvent.TOOLSELECTED, toolSelectedListener);
			result.addEventListener(ToolDeselectedEvent.TOOLDESELECTED, toolDeselectedListener);
			
			
			return result;
		}
		
		private function toolboxCloseListener(event : ToolBoxWindowEvent) : void
		{
			if (toolbox == event.target)
			{
				hideToolbox();
			}
			else
			{
				sendNotification(ApplicationConstants.GENERAL_ERROR);
			}
		}
		
		private function toolSelectedListener(event : ToolSelectedEvent) : void
		{
			sendNotification(ApplicationConstants.TOOL_SELECTED, event.toolID);
		}
		
		private function toolDeselectedListener(event : ToolDeselectedEvent) : void
		{
			sendNotification(ApplicationConstants.TOOL_DESELECTED, event.toolID);
		}
		
		protected function hideToolbox() : void
		{
			if (toolbox != null && toolbox.isPopUp)
			{
				PopUpManager.removePopUp(toolbox);
			}
			sendNotification(ApplicationConstants.TOOLBOX_HIDDEN);
		}
		
		protected function removeToolbox() : void
		{
			if (toolbox != null)
			{
				if (toolbox.hasEventListener(ToolBoxWindowEvent.CLOSE))
				{
					saveWindow.removeEventListener(ToolBoxWindowEvent.CLOSE, windowCloseListener);
				}
				if (saveWindow.hasEventListener(ToolSelectedEvent.TOOLSELECTED))
				{
					saveWindow.removeEventListener(ToolSelectedEvent.TOOLSELECTED, toolSelectedListener();
				}
				if (saveWindow.hasEventListener(ToolDeselectedEvent.TOOLDESELECTED))
				{
					saveWindow.removeEventListener(ToolDeselectedEvent.TOOLDESELECTED, toolDeselectedListener);
				}
				if (toolbox.isPopUp)
				{
					PopUpManager.removePopUp(toolbox);
				}
				toolbox = null;
			}
		}
		
		protected function positionToolbox(x : Number = NaN, y: Number = NaN) : void
		{
			if (toolbox != null)
			{
				if (x != NaN)
				{
					toolbox.x = x;
				}
				if (y != NaN)
				{
					toolbox.y = y;
				}
			}			
		}
	}
}